# -*- coding: utf-8 -*-
"""
Created on Fri Aug 18 15:58:13 2017
@author: JClian
"""
import re
import bs4
import urllib.request  
from bs4 import BeautifulSoup 
import urllib.parse
import sys

def searchbaike(text):
        if '的意思' in text:
                text = text.split('的意思')[0]
        if '是什么' in text:
                text = text.split('是什么')[0]
    #search_item = input("Enter what you want(Enter 'out' to exit):")
    #while search_item != 'out':
        #if search_item == 'out':
        #    exit(0)
        #print("please wait...")
        result = ""
        try:
            url = 'https://baike.baidu.com/item/'+urllib.parse.quote(text)
            html = urllib.request.urlopen(url)  
            content = html.read().decode('utf-8')
            html.close()
            soup = BeautifulSoup(content, "lxml")  
            text = soup.find('div', class_="lemma-summary").children
            #print("search result:")
            for x in text:
                word = re.sub(re.compile(r"<(.+?)>"),'',str(x))
                words = re.sub(re.compile(r"\[(.+?)\]"),'',word)
                #print(words,'\n')
                content = words.strip()
                if len(content):
                    result+=content
        except AttributeError:
            #print("Failed!Please enter more in details!")
            return ""
        #search_item = input("Enter what you want(Enter 'out' to exit):")
        return result.strip()
 
